package com.jt.tenant.dialog;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.activity.CarInfoActivity;
import com.jt.tenant.activity.EquipInfoActivity;
import com.jt.tenant.activity.ScannerActivity;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.SetAddExpense;
import com.jt.tenant.fragment.CarFragment;
import com.jt.tenant.widget.CustomSpinner;
import com.jt.tenant.widget.ExpenseTypeSpinner;
import com.jt.tenant.widget.Tools;
import com.jt.tenant.widget.TypeSpinner;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/5.
 */
public class AddExpenseDialog extends JTDialog implements View.OnClickListener {

    private EditText et_message;

    private TextView tv_cancel, tv_submit;

    private ImageView close;

    private ExpenseTypeSpinner mSpinner;

    private List<String> mList = new ArrayList<>();

    private String type_select = "保养";

    private SetAddExpense setAddExpense = new SetAddExpense(new AsyCallBack<SetAddExpense.Info>(){

        @Override
        public void onSuccess(String toast, int type, SetAddExpense.Info info) throws Exception {

            if(info.msg.equals("000")){

                UtilToast.show(getContext(),"添加记录成功");

                if(CarFragment.refreshLinster != null){

                    CarFragment.refreshLinster.refresh();

                }

                dismiss();

            }else {

                UtilToast.show(getContext(),info.msg);

            }


        }
    });

    public AddExpenseDialog(Context context) {

        super(context);

        setContentView(R.layout.dialog_add_expense);

        initView();

    }

    private void initView() {

        (tv_cancel = (TextView) findViewById(R.id.id_cancel)).setOnClickListener(this);

        (tv_submit = (TextView) findViewById(R.id.id_confirm)).setOnClickListener(this);

        (close = (ImageView) findViewById(R.id.iv_dialog_close)).setOnClickListener(this);

        mSpinner = (ExpenseTypeSpinner) findViewById(R.id.spinner_expense_type);

        mSpinner.setString("保养");

        mSpinner.ClearSpinner();

        mList.add("保养");

        mList.add("保险");

        mList.add("理财");

        mSpinner.createItem(mList);

        mSpinner.setOnItemClickListener(new TypeSpinner.OnSpinnerClickListener<String>() {
            @Override
            public void onShow() {

                mSpinner.dismiss();

            }

            @Override
            public void onItemClick(String s) {

                type_select = s;

            }
        });

        et_message = (EditText) findViewById(R.id.id_message);

    }

    public void setHint(String hint) {

        et_message.setHint(hint);

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.iv_dialog_close:

                this.dismiss();

                break;

            case R.id.id_cancel:


                this.dismiss();

                break;

            case R.id.id_confirm:

                if(!et_message.getText().toString().trim().equals("")||et_message.getText() != null) {

                    setAddExpense.name = type_select;

                    setAddExpense.time = Tools.getTime2(String.valueOf(System.currentTimeMillis()));

                    setAddExpense.price = Tools.F_num(et_message.getText().toString().trim());

                    setAddExpense.execute(getContext());
                }else {

                    UtilToast.show(getContext(),"请输入准确消费金额");

                }

                break;

        }

    }
}
